package com.ggk911.pharmacy_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggk911.pharmacy_system.entity.DrugInfoType;
import com.ggk911.pharmacy_system.service.impl.DrugInfoTypeServiceImpl;
import com.ggk911.pharmacy_system.util.RestResult;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 药品种类控制层
 *
 * @author ggk-911
 * @version 1.0 2023/3/19 18:39
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@CrossOrigin
@RequestMapping("/drugInfoType")
@Api("药品分类信息控制层")
public class DrugInfoTypeController {
    private final DrugInfoTypeServiceImpl service;

    //GET查询所有类别
    @GetMapping("/queryAll")
    public RestResult queryAll(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size,
                               @RequestParam(defaultValue = "") String keyName) {
        Page<DrugInfoType> drugInfoTypePage = new Page<>(page, size);
        QueryWrapper<DrugInfoType> wrapper = new QueryWrapper<>();

        wrapper.like(!"".equals(keyName), "drug_info_type_name", keyName);

        service.page(drugInfoTypePage, wrapper);

        return RestResult.ok("查询成功", drugInfoTypePage);
    }

    //GET获取所有数据集合
    @GetMapping("/list")
    public RestResult getList(@RequestParam(defaultValue = "") String keyName) {
        QueryWrapper<DrugInfoType> wrapper = new QueryWrapper<>();
        wrapper.like(!"".equals(keyName), "drug_info_type_name", keyName);
        List<DrugInfoType> drugInfoTypeList = service.list(wrapper);
        return RestResult.ok("获取成功", drugInfoTypeList);
    }


    //PUT添加
    @PutMapping("/insert")
    public RestResult insert(DrugInfoType drugInfoType) {
        if (service.save(drugInfoType)) {
            return RestResult.ok("添加成功，返回编号：" + drugInfoType.getDrugInfoTypeId(), drugInfoType.getDrugInfoTypeId());
        }
        return RestResult.error("添加失败");
    }

    //DELETE删除
    @DeleteMapping("/delete")
    public RestResult delete(Integer id) {
        if (service.removeById(id)) {
            return RestResult.ok("删除成功");
        }
        return RestResult.error("删除失败");
    }

    //POST更新
    @PostMapping("/update")
    public RestResult update(DrugInfoType drugInfoType) {
        if (service.updateById(drugInfoType)) {
            return RestResult.ok("更新成功");
        }
        return RestResult.error("更新失败");
    }
}
